package com.googlecode.flightreports.countrysummary;

import java.util.Iterator;
import java.io.IOException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.googlecode.kohmori.reportgen.AbstractXMLBuilder;

public class ReportXMLBuilder extends AbstractXMLBuilder {
  
    public void parse(InputSource input) throws IOException, SAXException {
        handler.startDocument();
        generateFor((ReportObject)input);
        handler.endDocument();        
    }
    
    protected void generateFor(ReportObject report) throws SAXException {       
        handler.startElement("countrySummaryReport");

        handler.element("runDate", report.getRunDate(), runDateFormat);

        handler.startElement("countries");
        Iterator i = report.getCountries().iterator();
        while (i.hasNext()) {
        	ReportObject.Country item 
                    = (ReportObject.Country) i.next();
            generateFor(item);
        }
        handler.endElement("countries");
        handler.endElement("countrySummaryReport");
    }

    protected void generateFor(ReportObject.Country item) throws SAXException {
        handler.startElement("country");
        handler.element("name", item.getName());
        handler.element("isoCode", item.getISOCode());
        handler.element("region", item.getRegion());
        handler.startElement("cities");
        Iterator i = item.getCities().iterator();
        while (i.hasNext()) {
        	ReportObject.City city
                    = (ReportObject.City) i.next();
            generateFor(city);
        }
        handler.endElement("cities");
        handler.endElement("country");
    }
    
    protected void generateFor(ReportObject.City item) throws SAXException {
        handler.startElement("city");
        handler.element("name", item.getName());
        handler.element("language", item.getLanguage());
        handler.element("airport", item.getAirport());
        handler.element("arrivalCount", item.getArrivalCount());
        handler.element("departureCount", item.getDepartureCount());
        handler.endElement("city");
    }    
}
